package com.liu.udpChat;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceiver {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket=new DatagramSocket(9876);
        while (true){
//        准备接收包裹
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
            socket.receive(packet);//阻塞式接收包裹
//            拆分包裹
            String str=new String(packet.getData(),0,packet.getData().length);
//       如果对方发bye就结束聊天
            if (str.equals("bye")){
                break;
            }else {
                System.out.println(packet.getAddress()+":"+str);
            }
        }
        socket.close();
    }
}
